package com.shenbianyun.bosskgpay.service.pay;



import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.entity.RequestMessage;
import com.shenbianyun.bosskgpay.entity.pay.PaymentReqModel;
import com.shenbianyun.bosskgpay.utils.*;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description //Jmeter 批量付款 接口
 * 不可固定业务参数
 **/

public class TestPaymentServiceJ extends AbstractJavaSamplerClient {


    public final String CHARSET_UTF_8 = "UTF-8";
    public final Long MER_ID = 1623945517108708L;
    public static String MER_PRIVATE_KEY = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALsaWzg/FFuHnS2z0TlNmFo1M0KJ+rlrZUsiaZPBgd3qaT1uVRqISIYGWrV0EYP5tRLNz4G9u1NuFW60pdxN/n5utTkD5bNEGSXC/zauUMctVN8Ccdmm+HI9DXDkJ+oyEjOA8nSuNe+T+pzzYzdQYcU1rj95dmgRcVRFV4Ao8+JHAgMBAAECgYBfl2ChhQDWnpu8PNGZMD61X9bXmP0i/G6dC3DbT5MVGO05uIP8cUJ0TwzLBgjo34wWllX9GD17q5OBowPWYXayrCU4CsqVBwEejJb8WQURZMB4IUbqeJNRhcuMZguNCxpTAujpdLdRnGT94d+ZHMJt194o3FBMTikS38hAvzrvGQJBANsS/7oE0o+LdLkrp5iiEkKw4QNqDb0u9vot54oY5dak5pvS/GLoXDXUSGAMyKu7YH0/5NMKJKaP1TflbCA17hUCQQDao84UtpsxqobcBGCDzrk+0gr+7E38OuGe3306luchYNJfEHdfMuPHjGABwLMRthnGeeS8JJ5h1zxMnPlcw0HrAkB0VM1pCUCKqyIrZiwUGBQcR/Lkrj9nc3YA+blXEsuRxHUUz9/n4b19dCGOp5sz3kdpmzE2g89ch0pQ6UoWfKzVAkEAjHzay6Z2XFiFpVROrAMmNv6CQEGY7qzrEsnqTr5uL51mL92Fxq8Ne8hwXE/1ADlxoH3sgxRIjPffcB8hgACbFwJAfzrLuOd81YrlmxmLZ8Y1Q+ykMTn4zl94C/spU05Cmc9gI0/FLDqUR7yzTXv1kV32f3jg2rUuWC1A3uLhRnM+0g==";
    public static String MER_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC7Gls4PxRbh50ts9E5TZhaNTNCifq5a2VLImmTwYHd6mk9blUaiEiGBlq1dBGD+bUSzc+BvbtTbhVutKXcTf5+brU5A+WzRBklwv82rlDHLVTfAnHZpvhyPQ1w5CfqMhIzgPJ0rjXvk/qc82M3UGHFNa4/eXZoEXFURVeAKPPiRwIDAQAB";
    public final String INTER_KEY = "8193717818841741B1D303615DF36DE1";
    public final Long PROJECT_ID = 1610278112283306149L;
    public final String API_VERSION = "V1.0";
    public final String OMS_TEST_URL = "http://testgateway.serviceshare.com/testapi/clientapi/clientBusiness/common";
    public static Long levy_id = 35503L;//30481L、35503L
    public  RequestMessage rm;
    public  PaymentReqModel paymentReqModel;
    public  PaymentReqModel.PayItems payItems;
    public  List payItemsList;


    public  String runTestPaymentServiceJ() throws Exception {

        RequestMessage rm = new RequestMessage();
        //生成id码 理论上不重复
        Long id = SnowflakeIdGeneratorFactory.getInstance().nextId();
        rm.setReqId("test" + id);
        rm.setFunCode("6001");
        rm.setMerId(MER_ID);
        rm.setVersion(API_VERSION);



        /**--------————————————业务请求参数reqData——————————————————**/
        PaymentReqModel paymentReqModel = new PaymentReqModel();

        //商户批次号
        paymentReqModel.setMerBatchId("batch" + id );
        //服务商id
        paymentReqModel.setProviderId(levy_id);
        //任务id（项目id）
        paymentReqModel.setTaskId(PROJECT_ID);
        //批量付款payItems字段
        List payItemsList = new ArrayList();
        PaymentReqModel.PayItems payItems = new PaymentReqModel.PayItems();
        //收款人名称
        payItems.setPayeeName("崔方方");
        //身份证号
        payItems.setIdCard("412828199310044519");
        //收款人账号
        payItems.setPayeeAcc("6217000010071772738");
        //金额（分）
        payItems.setAmt(1L);
        //收款人手机号
        payItems.setMobile("18001150364");
        //备注
        payItems.setMemo("api请求");
        //商户订单号，同批次内确保唯一不可重复。
        payItems.setMerOrderId("order" + id);
        //付款方式 0：银行卡，1：支付宝，2：微信
        payItems.setPaymentType(0);
        /**异步通知地址**/
        //payItems.setNotifyUrl("http://192.168.40.78:8081/pay/notify");

        payItemsList.add(payItems);
        paymentReqModel.setPayItems(payItemsList);

        String json = JsonUtils.toJson(paymentReqModel);
//        System.out.println("请求未加密数据:" + json);

        //加密处理
        byte[] bs = DESUtils.encrypt(json.getBytes(CHARSET_UTF_8), INTER_KEY);
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, MER_PRIVATE_KEY));

        String reqStr = JsonUtils.toJson(rm);
        //System.out.println("请求数据：" + reqStr);

        //发送请求
        String resData = HttpClientHelper.sendPost(OMS_TEST_URL, "application/json", reqStr, "POST", CHARSET_UTF_8);
        //System.out.println("响应数据：" + resData);


        return resData;
    }


    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        SampleResult sampleResult = new SampleResult();
        String resData = "";
        try {
            sampleResult.sampleStart();
            resData = runTestPaymentServiceJ();
            //断言
            JSONObject jsonObject1 = JSONObject.parseObject(resData);
            if (jsonObject1.get("resCode").equals("0000")) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true + "", SampleResult.TEXT);
                System.out.println("批量付款结果:" + true);
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + " " + jsonObject1.get("resMsg"), SampleResult.TEXT);
                System.out.println("批量付款结果:" + false + " " + jsonObject1.get("resMsg"));
            }
        } catch (Exception e) {
            sampleResult.setSuccessful(false);
            sampleResult.setResponseData(resData, SampleResult.TEXT);
            sampleResult.setResponseMessage(e.toString());
        } finally {
            sampleResult.sampleEnd();
        }

        return sampleResult;
    }


    //public static void main(String[] args) {
    //    TestPaymentServiceJ j = new TestPaymentServiceJ();
    //    try {
    //        System.out.println(j.runTestPaymentServiceJ());
    //    } catch (Exception e) {
    //        e.printStackTrace();
    //    }
    //}

}
